/* eslint-disable jsdoc/check-tag-names */
'use strict';

const Controller = require('egg').Controller;

class CharacterController extends Controller {
  /**
     *
     * @api {POST} /api/character/create 创建人物
     * @apiName 创建人物
     * @apiGroup 人物模块
     * @apiVersion  1.0.0
     *
     *
     * @apiParam  {String} name 人物名字
     * @apiParam  {String} position  人物职位
     * @apiParam  {String} resume 人物履历
     *
     *
     * @apiParamExample  {type} Request-Example:
        {
            "name": "name",
            "position": "position",
            "resume": "resume"
        }
     *
     *
     * @apiSuccessExample {type} Success-Response:
            {
                "code": 200,
                "msg": "成功操作",
                "data": {
                    "id": 1,      //人物id
                    "name": "name", //人物名字
                    "position": "position", //人物职位
                    "resume": "resume", //人物履历
                    "projectId": 1, //项目id
                    "updatedAt": "2021-11-23T12:26:58.230Z",
                    "createdAt": "2021-11-23T12:26:58.230Z"
                }
            }
     *
     *
     */
  async create() {
    const { app, ctx, service } = this;
    const checkRes = new ctx.helper.checkData(
      this,
      'name',
      'position',
      'resume'
    ).checkDataRes;
    try {
      if (checkRes) {
        const res = await service.web.character.create();
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule(error);
    }
  }

  /**
     *
     * @api {DELETE} /api/character/delete 删除人物
     * @apiName 删除人物
     * @apiGroup 人物模块
     * @apiVersion  1.0.0
     *
     *
     * @apiParam  {Number} id 人物id
     *
     * @apiParamExample  {type} Request-Example:
        {
            "id":5
        }
     *
     *
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "删除成功",
            "data": []
        }
     *
     *
     */
  async delete() {
    const { app, ctx, service } = this;
    try {
      const res = await service.web.character.delete();
      if (res == 'id不存在,删除失败') {
        return ctx.helper.errorModule(res);
      }
      return ctx.helper.successModule(res);
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule(error);
    }
  }
  /**
    *
    * @api {PUT} /api/character/update 修改人物
    * @apiName 人物案例
    * @apiGroup 人物模块
    * @apiVersion  1.0.0
    *
    *
    * @apiParam  {Number} id 案例id
    * @apiParam  {String} name  人物名称
    * @apiParam  {String} position 人物描述
    * @apiParam  {String} resume 人物履历
    *
    *
    * @apiParamExample  {type} Request-Example:
        {
            "id":4,
            "name":"ww",
            "position":"技术总监",
            "resume":"北京大学毕业"
        }
    *
    *
    * @apiSuccessExample {type} Success-Response:
       {
           "code": 200,
           "msg": "修改成功",
           "data": []
       }
    *
    *
    */
  async update() {
    const { app, ctx, service } = this;
    const checkRes = new ctx.helper.checkData(
      this,
      'id',
      'name',
      'position',
      'resume'
    ).checkDataRes;

    try {
      if (checkRes) {
        const res = await service.web.character.update();
        if (res == 'id不存在,修改失败') {
          return ctx.helper.errorModule(res);
        }
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule('操作失败');
    }
  }

  /**
    *
    * @api {GET} /api/character/getAll?limit=10^offset=0 获取所有人物
    * @apiName 获取所有人物
    * @apiGroup 人物模块
    * @apiVersion  1.0.0
    *
    *
    * @apiParam  {Number} limit 人物返回数
    * @apiParam  {Number} offset 人物偏移数
    *
    *
    *
    * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "成功操作",
            "data": {
                "count": 2, //人物总数
                "rows": [
                    {
                        "id": 1,    //人物id
                        "name": "name", //人物名字
                        "position": "position", //人物职位
                        "resume": "resume", //人物履历
                        "projectId": 1,
                        "createdAt": "2021-11-23T12:26:58.000Z",
                        "updatedAt": "2021-11-23T12:26:58.000Z"
                    },
                    {
                        "id": 4,
                        "name": "ww",
                        "position": "技术总监",
                        "resume": "北京大学毕业",
                        "projectId": 1,
                        "createdAt": "2021-11-24T04:20:02.000Z",
                        "updatedAt": "2021-11-24T04:21:52.000Z"
                    }
                ],
                "projectName": "官网项目"
            }
        }
    *
    *
    */
  async getAll() {
    const { ctx, service } = this;

    try {
      const res = await service.web.character.getAll();
      return ctx.helper.successModule(res);
    } catch (error) {
      return ctx.helper.errorModule(error.message);
    }
  }

  /**
    *
    * @api {GET} /api/character/getById?id=1 根据id获取人物及其案例
    * @apiName 根据id获取人物及其案例
    * @apiGroup 人物模块
    * @apiVersion  1.0.0
    *
    *
    * @apiParam  {Number} id 人物id
    *
    * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "成功操作",
            "data": {
                "id": 1,    //人物id
                "name": "name", //人物名字
                "position": "position", //人物职位
                "resume": "resume", //人物履历
                "projectId": 1, //项目id
                "createdAt": "2021-11-23T12:26:58.000Z",
                "updatedAt": "2021-11-23T12:26:58.000Z",
                "Cases": [
                    {
                        "id": 69,   //案例id
                        "cover": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/_1636424851757_.1.jpg", //案例封面图
                        "name": "case", //案例名
                        "sketch": "sketch", //案例描述
                        "content": "content",   //案例内容
                        "characterId": 1,   //人物id
                        "projectId": 1,
                        "createdAt": "2021-11-23T12:27:49.000Z",
                        "updatedAt": "2021-11-23T12:27:49.000Z"
                    },
                    {
                        "id": 72,
                        "cover": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/_1636424851757_.1.jpg",
                        "name": "case",
                        "sketch": "sketch",
                        "content": "content",
                        "characterId": 1,
                        "projectId": 1,
                        "createdAt": "2021-11-24T03:24:13.000Z",
                        "updatedAt": "2021-11-24T03:24:13.000Z"
                    },
                    {
                        "id": 73,
                        "cover": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/_1636424851757_.1.jpg",
                        "name": "case",
                        "sketch": "sketch",
                        "content": "content",
                        "characterId": 1,
                        "projectId": 1,
                        "createdAt": "2021-11-24T03:24:59.000Z",
                        "updatedAt": "2021-11-24T03:24:59.000Z"
                    }
                ]
            }
        }
    *
    *
    */
  async getById() {
    const { ctx, service } = this;
    const checkRes = new ctx.helper.checkData(this, 'id').checkDataRes;

    // 如果有缺少参数
    if (checkRes) {
      const res = await service.web.character.getById();
      return ctx.helper.successModule(res);
    }
  }
}
module.exports = CharacterController;
